<?php

class WebAdsmanageSortController extends Controller {

    public $layout = 'main';

    /**
     * @var返回过滤数组
     * @author nirsh
     * @version 5.0
     * @demo 
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * @var返回验证条件 如果不需要验证权限 把$isPriv 设置为allow
     * @author nirsh
     * @version 5.0
     * @demo 
     *    switch($action){
     *      case "login":
     *          $isPriv = 'allow'
     *          break;
     *      default:
     *          $isPriv = $this->checkAccess($controller, $action);
     *      }
     */
    public function accessRules() {
        $controller = Yii::app()->getController()->id;
        $action = Yii::app()->getController()->action->id;
        //登录不需要判断权限
        $isPriv = $action == 'adsShow' ? 'allow' : $this->checkAccess($controller, $action);
        return array(
            array($isPriv,
                'actions' => array($action),
                'users' => array('*'),
            )
        );
    }

    /**
     * 访问某条具体的记录.
     * @param $id 某条记录的主键
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    /**
     * 获取广告位代码
     */
    function actionAdsShow() {
        $id = intval($_GET['id']);
        $smodel = $this->loadModel($id);
        if ($smodel->ads_start == 1) {
            switch ($smodel->ads_type) {
                case 1:
                    $aModel = WebAdsmanage::model()->find("sort='" . $id . "' AND state=1 ");
                    $imageurl = $this->createAbsoluteUrl($aModel->images);
                    $aImage = addslashes(trim(CHtml::image($imageurl, '', array('height' => $smodel->ads_height, 'width' => $smodel->ads_width))));
                    echo 'document.write("<a href=' . $aModel->url . '>' . $aImage . '</a>")' . ';';

                    break;
                case 2:
                    $aModel = WebAdsmanage::model()->findAll("sort='" . $id . "' AND state=1");
                    if (!empty($aModel)) {
                        foreach ($aModel as $v) {
                            $imageurl = $this->createAbsoluteUrl($v->images);

                            $dString.= "<li>" . CHtml::link(CHtml::image($imageurl, '', array('height' => $smodel->ads_height, 'width' => $smodel->ads_width, 'text' => $v->note)), $v->url) . "</li>";
                        }
                        //myfocus css路径
                        //$cssUrl = Yii::app()->createAbsoluteUrl('/js/myfocus/pattern/mF_YSlider.css');
                        $jString.= <<<EOT
                        <div id="ads_$id"><div class="loading"></div><ul class="pic">$dString</ul></div>
EOT;
                    }
                    echo 'document.write("' . addslashes(trim($jString)) . '")' . ';';
                    $jurl = Yii::app()->createAbsoluteUrl("js/myfocus/myfocus-2.0.4.min.js");
                    $purl = Yii::app()->createAbsoluteUrl("js/myfocus") . "/";
                    echo '$.getScript(\'' . $jurl . '\',function(){
                                $(\'#ads_' . $id . '\').myFocus({
                                pattern:\'mF_kdui\',
                                time:\'3\',
                                width:' . $smodel->ads_width . ',
                                height:' . $smodel->ads_height . ',
                                path:\'' . $purl . '\',
                                txtHeight:\'default\',
                                });
                            });';
                    break;
            }
        }
    }

    /*
     * 激活AJAX激活状态
     */

    public function actionSetImportant() {
        $id = intval($_REQUEST['id']);
        $model = $this->loadModel($id);
        $model->ads_start = $_REQUEST['ads_start'];
        //$model->resetState($_REQUEST['state']);
        $model->save();
    }

    /**
     * 创建动作
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionCreate() {
        $model = new WebAdsmanageSort;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['WebAdsmanageSort'])) {
            $model->attributes = $_POST['WebAdsmanageSort'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    /**
     * 更新数据 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['WebAdsmanageSort'])) {
            $model->attributes = $_POST['WebAdsmanageSort'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * 删除记录 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionDelete() {
        $sId = $_REQUEST['id'];
        if (is_array($sId)) {
            foreach ($sId as $id) {
                $tModel = $this->loadModel($id);
                $tModel->delete();
                WebAdsmanage::model()->deleteAllByAttributes(array('sort'=>$id));
            }
        } else {
            $tModel = $this->loadModel($sId);
            $tModel->delete();
        }
        if (isset($_REQUEST['ajax'])) {
            echo "菜单删除成功";
        } else {
            $this->redirect($this->rewriteCreateUrl('admin'));
        }
    }

    /**
     * 列表动作 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionIndex() {
        $model = new WebAdsmanageSort('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['WebAdsmanageSort']))
            $model->attributes = $_REQUEST['WebAdsmanageSort'];
        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * 数据管理
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionAdmin() {
        $model = new WebAdsmanageSort('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['WebAdsmanageSort']))
            $model->attributes = $_REQUEST['WebAdsmanageSort'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * 返回数据模型是基于主键所得到的变量 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function loadModel($id) {
        $model = WebAdsmanageSort::model()->findByPk((int) $id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * ajax验证
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'web-adsmanage-sort-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
